package ru.nsu.fit.client.core.commands;

import ru.nsu.fit.client.core.log_manager.realization.KernelLogManager;
import ru.nsu.fit.client.core.response.KernelResponse;
import ru.nsu.fit.client.core.soap.send_question.SendQuestion;
import ru.nsu.fit.client.core.soap.send_question.SendQuestionOutput;

import java.util.List;

public class SendQuestionCommand implements  IClientCommand {
    private final String questionText;
    private final List<String> questionOptions;
    private final long userSessionId;
    private final String serverUrl;

    public SendQuestionCommand(final String serverUrl, final long userSessionId, final String questionText, final List<String> questionOptions)
    {
        this.userSessionId = userSessionId;
        this.questionText = questionText;
        this.questionOptions = questionOptions;
        this.serverUrl = serverUrl;
    }

    @Override
    public KernelResponse execute() {
        try
        {
            KernelLogManager.print("Sending question...");
            SendQuestionOutput output = SendQuestion.execute(serverUrl, userSessionId, questionText, questionOptions);

            KernelLogManager.print("Sending question - got response!");
            KernelResponse response = new KernelResponse(false);
            response.setErrorCode(output.getErrorCode());
            response.setErrorText(output.getErrorMessage());

            return response;
        }
        catch (Exception e)
        {
            KernelLogManager.printf("Sending question - web service error: %s", e.getMessage());
            KernelResponse response = new KernelResponse(false);
            response.setErrorText(e.getMessage());
            return response;
        }
    }
}
